{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.获取数据\n",
    "# 2.数据基本处理\n",
    "# 2.1 合并表格\n",
    "# 2.2 交叉表合并\n",
    "# 2.3 数据截取\n",
    "# 3.特征工程 — pca\n",
    "# 4.机器学习（k-means）\n",
    "# 5.模型评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.cluster import KMeans\n",
    "from sklearn.metrics import silhouette_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.获取数据\n",
    "order_product = pd.read_csv(\"../../../../data/instacart/order_products__prior.csv\")\n",
    "products = pd.read_csv(\"../../../../data/instacart/products.csv\")\n",
    "orders = pd.read_csv(\"../../../../data/instacart/orders.csv\")\n",
    "aisles = pd.read_csv(\"../../../../data/instacart/aisles.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.数据基本处理\n",
    "# 2.1 合并表格\n",
    "table1 = pd.merge(order_product, products, on=[\"product_id\", \"product_id\"])\n",
    "table2 = pd.merge(table1, orders, on=[\"order_id\", \"order_id\"])\n",
    "table = pd.merge(table2, aisles, on=[\"aisle_id\", \"aisle_id\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(32434489, 14)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>order_id</th>\n",
       "      <th>product_id</th>\n",
       "      <th>add_to_cart_order</th>\n",
       "      <th>reordered</th>\n",
       "      <th>product_name</th>\n",
       "      <th>aisle_id</th>\n",
       "      <th>department_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>eval_set</th>\n",
       "      <th>order_number</th>\n",
       "      <th>order_dow</th>\n",
       "      <th>order_hour_of_day</th>\n",
       "      <th>days_since_prior_order</th>\n",
       "      <th>aisle</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>33120</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Organic Egg Whites</td>\n",
       "      <td>86</td>\n",
       "      <td>16</td>\n",
       "      <td>202279</td>\n",
       "      <td>prior</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "      <td>8.0</td>\n",
       "      <td>eggs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>26</td>\n",
       "      <td>33120</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>Organic Egg Whites</td>\n",
       "      <td>86</td>\n",
       "      <td>16</td>\n",
       "      <td>153404</td>\n",
       "      <td>prior</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>16</td>\n",
       "      <td>7.0</td>\n",
       "      <td>eggs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>120</td>\n",
       "      <td>33120</td>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>Organic Egg Whites</td>\n",
       "      <td>86</td>\n",
       "      <td>16</td>\n",
       "      <td>23750</td>\n",
       "      <td>prior</td>\n",
       "      <td>11</td>\n",
       "      <td>6</td>\n",
       "      <td>8</td>\n",
       "      <td>10.0</td>\n",
       "      <td>eggs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>327</td>\n",
       "      <td>33120</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>Organic Egg Whites</td>\n",
       "      <td>86</td>\n",
       "      <td>16</td>\n",
       "      <td>58707</td>\n",
       "      <td>prior</td>\n",
       "      <td>21</td>\n",
       "      <td>6</td>\n",
       "      <td>9</td>\n",
       "      <td>8.0</td>\n",
       "      <td>eggs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>390</td>\n",
       "      <td>33120</td>\n",
       "      <td>28</td>\n",
       "      <td>1</td>\n",
       "      <td>Organic Egg Whites</td>\n",
       "      <td>86</td>\n",
       "      <td>16</td>\n",
       "      <td>166654</td>\n",
       "      <td>prior</td>\n",
       "      <td>48</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>9.0</td>\n",
       "      <td>eggs</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   order_id  product_id  add_to_cart_order  reordered        product_name  \\\n",
       "0         2       33120                  1          1  Organic Egg Whites   \n",
       "1        26       33120                  5          0  Organic Egg Whites   \n",
       "2       120       33120                 13          0  Organic Egg Whites   \n",
       "3       327       33120                  5          1  Organic Egg Whites   \n",
       "4       390       33120                 28          1  Organic Egg Whites   \n",
       "\n",
       "   aisle_id  department_id  user_id eval_set  order_number  order_dow  \\\n",
       "0        86             16   202279    prior             3          5   \n",
       "1        86             16   153404    prior             2          0   \n",
       "2        86             16    23750    prior            11          6   \n",
       "3        86             16    58707    prior            21          6   \n",
       "4        86             16   166654    prior            48          0   \n",
       "\n",
       "   order_hour_of_day  days_since_prior_order aisle  \n",
       "0                  9                     8.0  eggs  \n",
       "1                 16                     7.0  eggs  \n",
       "2                  8                    10.0  eggs  \n",
       "3                  9                     8.0  eggs  \n",
       "4                 12                     9.0  eggs  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.2 交叉表合并\n",
    "table = pd.crosstab(table[\"user_id\"], table[\"aisle\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(206209, 134)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 2.3 数据截取\n",
    "# new_table = table[:1000]\n",
    "# new_table.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.特征工程 — pca\n",
    "# 3.1 创建PCA（主成分分析）转换器\n",
    "transfer = PCA(n_components=0.9)\n",
    "\n",
    "# 3.2 特征降维\n",
    "trans_data = transfer.fit_transform(table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(206209, 27)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trans_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4.机器学习（k-means）\n",
    "# 4.1 创建KMeans估计其\n",
    "estimator = KMeans(n_clusters=15)\n",
    "\n",
    "# 4.2 训练模型并预测\n",
    "y_pre = estimator.fit_predict(trans_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.24103013956960584"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5.模型评估\n",
    "# 5.1 轮廓特征SC取值为[-1, 1], 越接近1越好\n",
    "silhouette_score(trans_data, y_pre)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
